package com.press.controllers.admin;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.UserBean;
import com.press.services.PersonalDataService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/5.
 */
@Controller
@RequestMapping("/PersonalData")
public class PersonalDataController {
    private static Logger log = Logger.getLogger(PersonalDataController.class);
    @Resource(name = "personalDataService")
    private PersonalDataService personalDataService;

    public PersonalDataService getPersonalDataService() {
        return personalDataService;
    }

    public void setPersonalDataService(PersonalDataService personalDataService) {
        this.personalDataService = personalDataService;
    }

    @RequestMapping("/setAdminInfo")
    public void setAdminInfo(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Map<String,String[]> para = request.getParameterMap();
        UserBean adminUser = new UserBean();
        try {
            Class<?> admin = Class.forName("com.press.bean.UserBean");
            for(String name : para.keySet()){
                log.info(para.get(name)[0]);
                Method method = admin.getMethod("setUser_" + name, new Class[]{String.class});
                method.invoke(adminUser,new Object[]{para.get(name)[0]});
            }
            adminUser.setUser_id(((UserBean)request.getSession().getAttribute("admin")).getUser_id());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            log.error("类没找到！");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            log.error("方法没找到！");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            log.error("安全权限异常！");
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if(resolver.isMultipart(request)){
            MultipartHttpServletRequest multiRequest =  (MultipartHttpServletRequest)request;
            MultipartFile multiFile = multiRequest.getFile("avatar");
            if(!multiFile.isEmpty()) {
                String filename = multiFile.getOriginalFilename();
                filename = "avatar_"+adminUser.getUser_id()+filename.substring(filename.lastIndexOf("."));
                String path = request.getServletContext().getRealPath("/") + "/static/file/avatar/";
                File file = new File(path,filename);
                if(!file.exists()){
                    file.createNewFile();
                }
                multiFile.transferTo(file);
                adminUser.setUser_avatar("/static/file/avatar/" + filename);
            }
        }
        adminUser = personalDataService.updateAdmin(adminUser);
        adminUser.setUser_password(null);
        request.getSession().setAttribute("admin",adminUser);
        log.info("管理员信息修改成功！");
        //request.getRequestDispatcher("/WEB-INF/admin/personalData.jsp").forward(request,response);
        response.sendRedirect(request.getContextPath()+"/admin/personalData.jsp");
    }
    @RequestMapping("/setIntroduction")
    public void setIntroduction(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String introduction = request.getParameter("introduction");
        String content = request.getParameter("content");
        UserBean admin = new UserBean();
        admin.setUser_introduction(introduction);
        admin.setUser_introduction_content(content);
        boolean msg = personalDataService.setAdminIntroduction(admin);
        if(msg){
            request.getSession().setAttribute("admin",personalDataService.getAdmin());
        }
        JSONObject json = new JSONObject();
        json.put("Msg",msg);
        log.info(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
    @RequestMapping("/getIntroduction")
    public void getIntroduction(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        if(request.getSession().getAttribute("admin")==null){
            response.getWriter().print("");
            return;
        }
        UserBean admin = personalDataService.getAdminIntroduction();
        JSONObject json = new JSONObject();
        json.put("content",admin.getUser_introduction_content());
        log.info(json.toJSONString());
        response.getWriter().print(json.toJSONString());
    }
}
